package com.sdp.right.utils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.util.StringUtils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

public class T2 {
	public static void main(String[] args) {
//		SysEmp emp = new SysEmp();
//		emp.setEmpName("11");
//		emp.setCreateTime(LocalDateTime.now());
//		Map<String, Object> m = BeanUtil.beanToMap(emp);
//		LocalDateTime l = (LocalDateTime) m.get("createTime");
//		System.out.println(LocalDateUtil.localDateTime2String(l));
//		System.out.println("MAC Address: " + getMacAddress());
		
		
		 // 文件路径
        String codeFileName = "D:\\code.xlsx";
        String dataFileName = "D:\\data.xlsx";
        String outputFileName = "D:\\new-data.xlsx";

        List<CodeEntity> codeList = new ArrayList<>();
        Map<String,DataEntity> dataMap = new HashMap<String,DataEntity>();
       
        //读取code
        readCode(codeFileName, codeList);
        
        //读取原始数据并生成新的数据
        readData(dataFileName, dataMap,codeList,outputFileName);

     
    
	}
	
	public static void readCode(String codeFileName, List<CodeEntity> codeList) {
		EasyExcel.read(codeFileName, CodeEntity.class, new AnalysisEventListener<CodeEntity>() {

            // 每读取一行数据时调用该方法
            public void invoke(CodeEntity e, AnalysisContext context) {
            	codeList.add(e);
            }

            // Excel 读取结束后调用该方法
            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
            	/*
            	System.out.println("读取完成，生成新的文件");
            	EasyExcel.write(outputFileName, CodeEntity.class)
                .sheet(0)  
                .doWrite(list); 
                */
            }

        }).sheet(0).headRowNumber(0).doRead();
	}
	
	
	public static void readData(String dataFileName, Map<String,DataEntity> dataMap,List<CodeEntity> codeList,String outputFileName) {
		EasyExcel.read(dataFileName, DataEntity.class, new AnalysisEventListener<DataEntity>() {
            // 每读取一行数据时调用该方法
            public void invoke(DataEntity e, AnalysisContext context) {
            	if(!StringUtils.isEmpty(e.getC5())) {
            		if(dataMap.containsKey(e.getC5())) {
            			System.out.println("重复的code:"+e.getC5()+",重复的行:"+(context.getCurrentRowNum()+1));
            		}else {
            			dataMap.put(e.getC5(), e);
            		}
            		
            	}
            	
            }

            // Excel 读取结束后调用该方法
            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
          
            	System.out.println(context.getCurrentRowNum()+1);
            	List<DataEntity> newDataList = new ArrayList<>();
            	
            	for(CodeEntity codeEntity : codeList) {
            		String code = codeEntity.getCode();
            		if(dataMap.containsKey(code)) {
            			newDataList.add(dataMap.get(code));
            			dataMap.remove(code);
            		}
            	}
            	
            	System.out.println("读取完成，生成新的文件");
            	EasyExcel.write(outputFileName, CodeEntity.class)
                .sheet(0)  
                .doWrite(newDataList); 
            }

        }).sheet(0).headRowNumber(0).doRead();
	}
	

	static String formatTime(String inTime) {
		DateTimeFormatter inputFormatter1 = DateTimeFormatter.ofPattern("yyyy-M-d HH:mm");
		DateTimeFormatter inputFormatter2 = DateTimeFormatter.ofPattern("yyyy-M-d H:mm");
        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = null;
        try {
        	dateTime = LocalDateTime.parse(inTime, inputFormatter1);
		} catch (Exception e) {
			dateTime = LocalDateTime.parse(inTime, inputFormatter2);
		}
        if(dateTime==null) {
        	return null;
        }
        return dateTime.format(outputFormatter);
	}
	
	
	  
	
	public static class CodeEntity {
		
		private String code;

		public String getCode() {
			return code;
		}

		public void setCode(String code) {
			this.code = code;
		}
	     
    }
	
	public static class DataEntity{
		private String c1;
		private String c2;
		private String c3;
		private String c4;
		private String c5;
		private String c6;
		private String c7;
		public String getC1() {
			return c1;
		}
		public void setC1(String c1) {
			this.c1 = c1;
		}
		public String getC2() {
			return c2;
		}
		public void setC2(String c2) {
			this.c2 = c2;
		}
		public String getC3() {
			return c3;
		}
		public void setC3(String c3) {
			this.c3 = c3;
		}
		public String getC4() {
			return c4;
		}
		public void setC4(String c4) {
			this.c4 = c4;
		}
		public String getC5() {
			return c5;
		}
		public void setC5(String c5) {
			this.c5 = c5;
		}
		public String getC6() {
			return c6;
		}
		public void setC6(String c6) {
			this.c6 = c6;
		}
		public String getC7() {
			return c7;
		}
		public void setC7(String c7) {
			this.c7 = c7;
		}
		
		
	}
}
